package conchmusic.conchmusicclient.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.SectionIndexer;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import conchmusic.conchmusicclient.R;
import conchmusic.conchmusicclient.pojo.Song;
import conchmusic.conchmusicclient.pojo.SortMusic;
import conchmusic.conchmusicclient.popupWindow.SelectPicPopupWindow;
import conchmusic.conchmusicclient.util.SongGetScanMusic;

/**
 * Created by Administrator on 2017/4/7.
 */

public class LocalListAdapter extends BaseAdapter implements SectionIndexer{
    private Context context;
    private List<Song> listItems;
    private LayoutInflater listContainer;
    private SelectPicPopupWindow menuWindow;
    private Activity activity;
    private SongGetScanMusic songGetScanMusic;
    private ArrayList<SortMusic> sortMusics;
    public LocalListAdapter(Context context, List<Song> listItems) {
        this.context = context;
        listContainer = LayoutInflater.from(context);
        this.listItems = listItems;
        this.activity=(Activity)context;
        Log.i("activity",context.getClass().getSimpleName());
        songGetScanMusic=new SongGetScanMusic(listItems);
        sortMusics=new ArrayList<>();
        sortMusics=songGetScanMusic.getmSortList();
    }

    @Override
    public Object[] getSections() {
        return null;
    }
//第一次出现该首字母的位置,也就是当前分组所在的位置
    @Override
    public int getPositionForSection(int sectionIndex) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = sortMusics.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == sectionIndex) {
                return i;
            }
        }
        return -1;
    }
//据ListView的position来找出当前位置所在的分组
    @Override
    public int getSectionForPosition(int position) {
        return sortMusics.get(position).getSortLetters().charAt(0);
    }

    public final class ListItemView{                //自定义控件集合
        public TextView localSongName;
        public TextView LocalSinger;
        public ImageButton imageButton;
        public TextView tvLetter;
    }
    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {

        return listItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SortMusic sortMusic=sortMusics.get(position);
        ListItemView  listItemView = null;
        if (convertView == null) {
            listItemView = new ListItemView();
            convertView = listContainer.inflate(R.layout.local_basic_music, null);
            listItemView.localSongName=(TextView) convertView.findViewById(R.id.text_view_name);
            listItemView.LocalSinger=(TextView) convertView.findViewById(R.id.text_view_info);
            listItemView.imageButton=(ImageButton) convertView.findViewById(R.id.image_button_action);
            convertView.setTag(listItemView);
            listItemView.tvLetter=(TextView)convertView.findViewById(R.id.tv_catagory);
        }else {
            listItemView = (ListItemView)convertView.getTag();
        }
        List<String> artistNamesList=listItems.get(position).getArtistNames();
        String artistName="";
        for(String artist:artistNamesList){
            artistName=artistName+artist+" ";
        }
        listItemView.LocalSinger.setText(artistName);
        listItemView.localSongName.setText((String) listItems.get(position).getTitle());
        listItemView.imageButton.setImageResource(R.drawable.ic_list_item_action);
        listItemView.imageButton.setTag(position);
        addListener(listItemView.imageButton);
        int section = getSectionForPosition(position);
        if (position == getPositionForSection(section)) {
            listItemView.tvLetter.setVisibility(View.VISIBLE);
            listItemView.tvLetter.setText(sortMusic.getSortLetters());
        } else {
            listItemView.tvLetter.setVisibility(View.GONE);
        }
        return convertView;
    }
    public void addListener(ImageButton imageButton){
        final int posi=(Integer) imageButton.getTag();
        imageButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        menuWindow= new SelectPicPopupWindow(LocalListAdapter.this,activity,posi,listItems);
                        menuWindow.showAtLocation(activity.findViewById(R.id.localMusic), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
                    }
                }

        );
    }


}
